home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / demos / mfgdemo / mfg_dsp.c < prev    next >
C/C++ Source or Header  |  1997-05-08  |  16KB  |  567 lines

  1. #ifndef lint
  2. static char SccsId[]= "@(#)mfg_dsp.c    V1.14    3/15/95";
  3. #endif
  4.  
  5. /*------------------------------------------------------------------
  6. | file name -- display.c
  7. |
  8. | functions        Description
  9. | ---------             -----------
  10. | InitDisplays        Initializes the SCREEN and DRAWPORTS
  11. | TermDisplays          Cleans up the SCREEN, DRAWPORTS and DISPLAY_LIST
  12. |
  13. | SwitchDisplay      Display Manager
  14. | OverlayPidDisplay        Overlay the view.
  15. | RemovePidDisplay         Deletes the overlayed view.
  16. |
  17. | preload_views        Preloads all views... (base and overlays)
  18. | create_drawport    Creates the drawport and add is to the table
  19. | get_drawport        Returns the previously or newly created drawport.
  20. | load_view        Loads the view and adds it to the table.
  21. | next_display       Adds a display to the list.
  22. | prev_display          Deletes a display from the list.
  23. | reset_display         Destroys the display list, reset to the top display
  24. | create_display_list   Create a DisplayList for NEXT/PREV.
  25. | del_display_list_item    Delete a display from the DisplayList.
  26. | destroy_display_list  Destroys the DisplayList.
  27. |
  28. |-----------------------------------------------------------------*/
  29.  
  30. #include "std.h"
  31. #include "dvstd.h"
  32. #include "dvGR.h"
  33. #include "Tfundecl.h"
  34. #include "mfg_vars.h"
  35. #include "VOfundecl.h"
  36. #include "VTfundecl.h"
  37. #include "VUfundecl.h"
  38. #include "GRfundecl.h"
  39. #include "mfg_fundecl.h"
  40.  
  41.  
  42.  
  43. #define DEFAULT_SIZE     (RECTANGLE*)NULL
  44. #define DEFAULT_PORTION (RECTANGLE*)NULL
  45.  
  46. #define NEW_LIST        (char)0
  47. #define ADD_TO_LIST        (char)1
  48. #define DELETE_FROM_LIST    (char)2
  49.  
  50. /***************** Begin Function Declarations *************/
  51. LOCAL  void preload_views V_P_((void));
  52. LOCAL  DRAWPORT create_drawport V_P_((OBJECT screen, char *viewname, RECTANGLE *where_vp));
  53. LOCAL  VIEW load_view V_P_((char *view_name));
  54. LOCAL  DRAWPORT get_drawport V_P_((char *view_name));
  55. LOCAL  DRAWPORT next_display V_P_((char *view_name));
  56. LOCAL  DRAWPORT prev_display V_P_((void));
  57. LOCAL  DRAWPORT reset_display V_P_((void));
  58. LOCAL  DISPLAY_LIST *create_display_list V_P_((void));
  59. LOCAL  void del_display_list_item V_P_((DISPLAY_LIST *display_item));
  60. LOCAL  void destroy_display_list V_P_((DISPLAY_LIST **display_info));
  61. /***************** End Function Declarations *************/
  62.  
  63. /*-----------------------------------------------------------------
  64. |
  65. |  InitDisplays
  66. |       Performs the initialization needed for the display.
  67. |
  68. |       A SCREEN is opened using DVDEVICE and other attributes.
  69. |
  70. |    create_drawport() is called to create a drawport for each
  71. |    display area. create_drawport will be called each time a
  72. |    a new views is needed by the display manager.
  73. */
  74. void 
  75. InitDisplays (device)
  76.      char *device;
  77. {
  78.    int error_code;
  79.    char buf[50];
  80.   
  81.   /* Open the main window */
  82.   VUoff_copyright ();
  83.   DVscreen = TscOpenSet (device, (CHAR *) NULL,
  84.                V_WINDOW_NAME, " *** DataViews Manufacturing Demo ***",
  85. #ifdef WINNT
  86.              V_WIN32_ICON_NAME,      "mfgicon",
  87. #ifdef DOUBLE_BUFFER
  88.              V_WIN32_DOUBLE_BUFFER,  YES,
  89. #endif /* DOUBLE_BUFFER */
  90. #else  /* Not WINNT */
  91.              V_X_EXPOSURE_BLOCK,    YES,
  92. #endif /* WINNT */
  93.              V_END_OF_LIST );
  94.  
  95. if(!DVscreen)
  96.  {
  97.    error_code=TscOpenError();
  98. #ifdef WINNT
  99.    sprintf(buf,"Product is not validated. Error code %d.",error_code);
  100.    MessageBox(NULL,buf,"Validation Error",MB_OK);
  101. #else
  102.    fprintf(stderr,"Product is not validated. Error code %d.",error_code);
  103. #endif
  104.    exit(error_code);
  105.   }
  106.  
  107.   /* If we couldn't open a window, exit the program */
  108.   EXIT_IF_INVALID (DVscreen, NO_DEVICE);
  109.  
  110.   /* Initialize the window events */
  111.   InitSimpleEvents ();
  112.  
  113.   /* Erase the "copyright" message and set the cursor */
  114.   (VOID) TscErase (DVscreen);
  115.  
  116.   /* Create the drawport table needed for display management */
  117.   DpTable = VTstcreate ("View Names and DRAWPORTs", (VTSTCOMPAREFUNPTR)NULL);
  118.  
  119.   /* Preload all the views... draws the top view  */
  120.   preload_views ();
  121.  
  122.   /* Set the active drawport to the Top View */
  123.   ActiveDrawport = get_drawport (ViewName[TOP_VIEW]);
  124.   EXIT_IF_INVALID (ActiveDrawport, NO_TOP_VIEW);
  125.  
  126.   (VOID) GRset (V_ACTIVE_CURSOR, V_END_OF_LIST);
  127.  
  128. }
  129.  
  130. /*-----------------------------------------------------------------
  131. |
  132. |  SwitchDisplays
  133. |       Switches the view being displayed in the MONITOR AREA.
  134. |
  135. |    The views are managed in a "DisplayList" which is
  136. |    updated according to the "choice".
  137. |       next_display() - Adds a display to the list.
  138. |       prev_display() - Deletes a display from the list.
  139. |       reset_display()- Resets the list back to the TOP_VIEW.
  140. |
  141. |    This allows us to keep a history of where we've been.
  142. |
  143. |    The old display is terminated before the new display is
  144. |    drawn;
  145. */
  146. void 
  147. SwitchDisplay (choice, view_name)
  148.      int choice;
  149.      char *view_name;
  150. {
  151.   DRAWPORT new_dp = NULL;
  152.  
  153.   /* If they switch to the top display, reset the NEXT/PREV list */
  154.   if (view_name && STRINGS_ARE_EQUAL (view_name, ViewName[TOP_VIEW]))
  155.     choice = RESET_DISPLAY;
  156.  
  157.   /* Switch displays according to the choice */
  158.   switch ((INT) choice)
  159.     {
  160.     case PREV_DISPLAY:
  161.       new_dp = prev_display ();
  162.       break;
  163.     case NEXT_DISPLAY:
  164.       new_dp = next_display (view_name);
  165.       break;
  166.     case RESET_DISPLAY:
  167.       new_dp = reset_display ();
  168.       break;
  169.     default:
  170.       break;
  171.     }
  172.  
  173.   /* If we can successfully switch displays, cleanup and continue */
  174.   if (new_dp)
  175.     {
  176.       /* Erase the old display */
  177.       (VOID) TdpErase (ActiveDrawport);
  178.       if (ActiveDrawport == DrawportId[PAINT_SHOP_VIEW] && PidDrawport)
  179.         (VOID) TdpErase (PidDrawport);
  180.  
  181.       /* Draw the new display */
  182.       ActiveDrawport = new_dp;
  183.       (VOID) TdpDraw (ActiveDrawport);
  184.       if (ActiveDrawport == DrawportId[PAINT_SHOP_VIEW] && PidDrawport)
  185.         (VOID) TdpDraw (PidDrawport);
  186.     }
  187. }
  188.  
  189. /*-----------------------------------------------------------------
  190. |
  191. |  OverlayPidDisplay
  192. |    Tries to overlay a view to the current display.  The
  193. |    named view's drawport is displayed.  Only one drawport
  194. |    can be active at once.
  195. |
  196. */
  197. LOCAL INT PidCount = 0;
  198. LOCAL DRAWPORT PrevPidDrawport = 0;
  199.  
  200. void 
  201. OverlayPidDisplay (view_name)
  202.      char *view_name;
  203. {
  204.   DRAWPORT desired_drawport;
  205.  
  206.   desired_drawport = get_drawport (view_name);
  207.   if (desired_drawport)
  208.     {
  209.       if (desired_drawport != PidDrawport)
  210.         {
  211.           if (desired_drawport != PrevPidDrawport)
  212.             PidCount++;
  213.  
  214.           PrevPidDrawport = PidDrawport;
  215.           PidDrawport = desired_drawport;
  216.           (VOID) TdpDraw (PidDrawport);
  217.         }
  218.     }
  219. }
  220.  
  221. /*-----------------------------------------------------------------
  222. |
  223. |  RemovePidDisplay
  224. |    Removes the PidDrawport if it's active and restores the area
  225. |       below the drawport.
  226. |
  227. */
  228. void RemovePidDisplay 
  229. V_P_ ((void))
  230. {
  231.   RECTANGLE *vp, erase_vp;
  232.  
  233.   if (PidDrawport)
  234.     {
  235.       /* Get the Pid drawport's area */
  236.       vp = TdpGetScreenVp (PidDrawport);
  237.  
  238.       /* Convert the Pid's area to screen coordinates */
  239.       (VOID) GRvcs_to_scs (&vp->ll, &erase_vp.ll);
  240.       (VOID) GRvcs_to_scs (&vp->ur, &erase_vp.ur);
  241.  
  242.       /* Erase the Pid drawport */
  243.       (VOID) TdpErase (PidDrawport);
  244.  
  245.       /* Restore the obscured area */
  246.       (VOID) TscRedraw (DVscreen, &erase_vp);
  247.  
  248.       /* reset the Pid Counts */
  249.       PidCount--;
  250.       if (PidCount != 0)
  251.         PidDrawport = PrevPidDrawport;
  252.       else
  253.         PidDrawport = (DRAWPORT) NULL;
  254.       PrevPidDrawport = (DRAWPORT) NULL;
  255.  
  256.     }
  257. }
  258.  
  259. /*-----------------------------------------------------------------
  260. |
  261. |  preload_views
  262. |       The function Pre-Loads the views....
  263. */
  264. LOCAL void preload_views 
  265. V_P_ ((void))
  266. {
  267.   INT i;
  268.   VIEW view;
  269.   OBJECT dr, area_obj;
  270.   DRAWPORT dp;
  271.   RECTANGLE vp, dummy;
  272.  
  273.   /* For all the main views... load the view, rebind its data,
  274.   |  create a drawport, add the drawport to the symbol table.
  275.   |  Draw the top view!
  276.   */
  277.   for (i = 0; i < NUM_DISPLAYS; i++)
  278.     {
  279.       DrawportId[i] = create_drawport (DVscreen, ViewName[i], (RECTANGLE *) NULL);
  280.       if (i == 0)
  281.         TdpDraw (DrawportId[i]);
  282.     }
  283.  
  284.   /* For all the Pid views... create a drawport using the area
  285.   |  defined by the pid.area object in the PAINT_SHOP_VIEW.
  286.   */
  287.   dp = get_drawport (ViewName[PAINT_SHOP_VIEW]);
  288.   view = TdpGetView (dp);
  289.   dr = TviGetDrawing (view);
  290.   area_obj = TdrGetNamedObject (dr, "pid.area");
  291.   (VOID) VOobBox (area_obj, &vp, &dummy);
  292.   (VOID) TdpWorldToScreen (dp, &vp.ll, &vp.ll);
  293.   (VOID) TdpWorldToScreen (dp, &vp.ur, &vp.ur);
  294.   (VOID) GRscs_to_vcs (&vp.ll, &vp.ll);
  295.   (VOID) GRscs_to_vcs (&vp.ur, &vp.ur);
  296.   for (i = 0; i < NUM_PIDS; i++)
  297.     (VOID) create_drawport (DVscreen, PidName[i], &vp);
  298.   PidDrawport = NULL;
  299. }
  300.  
  301.  
  302. /*-----------------------------------------------------------------
  303. |
  304. |  create_drawport
  305. |       The function creates a drawport in the specified area to
  306. |       display the specified view.
  307. |
  308. |       The drawport is added to the symbol tables for easy
  309. |       access by the display manager routines.
  310. |
  311. |       We call RebindData() for all the dynamic objects. This will
  312. |       see if the variable is being controlled by the application and
  313. |       rebind it if necessary.
  314. |
  315. |       We call SetupButtons(). This will setup a Service Function for
  316. |       the button input objects.
  317. */
  318. LOCAL DRAWPORT 
  319. create_drawport (screen, viewname, where_vp)
  320.      OBJECT screen;
  321.      char *viewname;
  322.      RECTANGLE *where_vp;
  323. {
  324.   VIEW view;
  325.   DRAWPORT drawport = NULL;
  326.   RECTANGLE what_vp, dummy;
  327.  
  328.   /* Load the view */
  329.   view = load_view (viewname);
  330.  
  331.   if (view)
  332.     {
  333.       if (!where_vp)
  334.         drawport = TdpCreate (screen, view,
  335.                               DEFAULT_SIZE, DEFAULT_PORTION);
  336.       else
  337.         {
  338.           /* Get the bounding box around the objects in the view */
  339.           (VOID) VOobBox (TviGetDrawing (view), &what_vp, &dummy);
  340.           drawport = TdpCreateStretch (screen, view, where_vp, &what_vp);
  341.         }
  342.  
  343.       /* Call RebindData() to rebind to application data */
  344.       RebindData (view);        /* found in mfg_rebind.c */
  345.  
  346.       /* Call SetupButtons() to post service function for button handling */
  347.       SetupButtons (view);      /* found in mfg_events.c */
  348.  
  349.       /* Add the drawport to the table */
  350.       (VOID) VTstsninsert (DpTable, StrClone (viewname), (INT *) drawport);
  351.     }
  352.   return drawport;
  353. }
  354.  
  355. /*-----------------------------------------------------------------
  356. |
  357. |  load_view
  358. |       Loads the view and rebinds its data.  If the view can't be
  359. |    loaded, return NULL.
  360. */
  361. LOCAL VIEW 
  362. load_view (view_name)
  363.      char *view_name;
  364. {
  365.   VIEW view = NULL;
  366.  
  367.   /* Load the view and add it to the table */
  368.   view = TviLoad (view_name);
  369.   if (view)
  370.     {
  371.  
  372.       /* Rebind the dyanmic object's variables to real-time data */
  373.       RebindData (view);        /* found in mfg_rebind.c */
  374.  
  375.     }
  376.  
  377.   return view;
  378. }
  379.  
  380. /*-----------------------------------------------------------------
  381. |
  382. |  get_drawport
  383. |       Returns the drawport associated with the view_name. If the
  384. |       drawport doesn't exist, it creates one.
  385. */
  386. LOCAL DRAWPORT 
  387. get_drawport (view_name)
  388.      char *view_name;
  389. {
  390.   SYMNODE key;
  391.  
  392.   /* See if the drawport is already in the Drawport Table */
  393.   key = VTstkeyfind (DpTable, view_name);
  394.   if (key)
  395.     return (DRAWPORT) VTsnvalue (key);
  396.   else
  397.     return create_drawport (DVscreen, view_name, (RECTANGLE *) NULL);
  398. }
  399.  
  400. /*-----------------------------------------------------------------
  401. |
  402. |  next_display
  403. |    Tries to add a display to the DisplayList. If the view can't
  404. |    be displayed, returns DV_FAILURE, else DV_SUCCESS.
  405. |
  406. */
  407. LOCAL DRAWPORT 
  408. next_display (view_name)
  409.      char *view_name;
  410. {
  411.   DISPLAY_LIST *prev_display;
  412.   DRAWPORT dp;
  413.  
  414.   /* Get the drawport associated with the view */
  415.   dp = get_drawport (view_name);
  416.   if (dp)
  417.     {
  418.       /* If the list doesn't exist yet, create it.
  419.       |
  420.       |  Make the current list top the previous display. Then,
  421.       |  make the new display the list top. */
  422.       if (!DisplayList)
  423.         {
  424.           prev_display = create_display_list ();
  425.           prev_display->drawport = ActiveDrawport;
  426.         }
  427.       else
  428.         prev_display = DisplayList;
  429.  
  430.       DisplayList = create_display_list ();
  431.       DisplayList->drawport = dp;
  432.       DisplayList->prev = prev_display;
  433.     }
  434.  
  435.   return dp;
  436. }
  437.  
  438. /*-----------------------------------------------------------------
  439. |
  440. |  prev_display
  441. |    If their is a previous display, make it the top and
  442. |    deletes the current from the list.
  443. */
  444. LOCAL DRAWPORT prev_display 
  445. V_P_ ((void))
  446. {
  447.   DISPLAY_LIST *prev_display;
  448.  
  449.   /* Make sure we have a list and a previous display before
  450.   |  you make the previous display the top.
  451.   */
  452.   if (DisplayList && DisplayList->prev)
  453.     {
  454.       prev_display = DisplayList->prev;
  455.       del_display_list_item (DisplayList);
  456.       DisplayList = prev_display;
  457.       return DisplayList->drawport;
  458.     }
  459.   else
  460.     return (DRAWPORT) NULL;
  461. }
  462.  
  463. /*-----------------------------------------------------------------
  464. |
  465. |  reset_display
  466. |    Resets the Display List, deletes the previous list and makes
  467. |    the TOP_VIEW the beginning of the list.
  468. */
  469. LOCAL DRAWPORT reset_display 
  470. V_P_ ((void))
  471. {
  472.  
  473.   /* Destroy the DisplayList */
  474.   destroy_display_list (&DisplayList);
  475.  
  476.   /* return the Active Drawport to the Top View */
  477.   return get_drawport (ViewName[TOP_VIEW]);
  478.  
  479. }
  480.  
  481. /*-----------------------------------------------------------------
  482. |
  483. |  create_display_list - Allocates and initializes a DISPLAY_LIST.
  484. */
  485. LOCAL DISPLAY_LIST *create_display_list 
  486. V_P_ ((void))
  487. {
  488.   DISPLAY_LIST *display_info;
  489.  
  490.   display_info = (DISPLAY_LIST *) S_ALLOC ((LONG) sizeof (DISPLAY_LIST));
  491.   display_info->drawport = (DRAWPORT) NULL;
  492.   display_info->prev = (DISPLAY_LIST *) NULL;
  493.   return display_info;
  494. }
  495.  
  496. /*-----------------------------------------------------------------
  497. |
  498. |  del_display_list_item - Frees a DISPLAY_LIST item.
  499. */
  500. LOCAL void 
  501. del_display_list_item (display_item)
  502.      DISPLAY_LIST *display_item;
  503. {
  504.   S_FREE ((CHAR *) display_item);
  505. }
  506.  
  507. /*-----------------------------------------------------------------
  508. |
  509. |  destroy_display_list - Destroys the DISPLAY_LIST by freeing each item.
  510. */
  511. LOCAL void 
  512. destroy_display_list (display_info)
  513.      DISPLAY_LIST **display_info;
  514. {
  515.   DISPLAY_LIST *info, *prev;
  516.  
  517.   info = *display_info;
  518.   while (info)
  519.     {
  520.       prev = info->prev;
  521.       del_display_list_item (info);
  522.       info = prev;
  523.     }
  524.   *display_info = NULL;
  525. }
  526.  
  527. /*-----------------------------------------------------------------
  528. |
  529. |  TermDisplays
  530. |       Performs the termination and clean up needed for the display
  531. |    components. This balances InitDisplays().
  532. |
  533. |    The Drawport and View Tables are "traversed" to clean up
  534. |    the views and drawports.  We also clean up the table as we go.
  535. |    The AreaCoords used to define drawport areas are "freed". The
  536. |    display manager's DisplayList is destroyed. Finally, the screen
  537. |    is erased and closed.
  538. */
  539. void TermDisplays 
  540. V_P_ ((void))
  541. {
  542.   CHAR *viewname;
  543.   SYMNODE node;
  544.   VIEW view;
  545.   DRAWPORT dp;
  546.  
  547.   /* Destroy the display info */
  548.   while (VTstlen (DpTable) > 0)
  549.     {
  550.       node = VTstsnget (DpTable, 0);
  551.       viewname = VTsnkey (node);
  552.       S_FREE (viewname);
  553.       dp = (DRAWPORT) VTsnvalue (node);
  554.       view = TdpGetView (dp);
  555.       (VOID) TdpDestroy (dp);
  556.       (VOID) TviDestroy (view);
  557.       VTstsnremove (DpTable, node);
  558.     }
  559.   VTstdestroy (DpTable);
  560.  
  561.   /* Destroy the Display List */
  562.   destroy_display_list (&DisplayList);
  563.  
  564.   /* Destroy, Erase and Close the Windows */
  565.   (VOID) TscClose (DVscreen);
  566. }
  567.